﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestaurateurConsole;

namespace RestaurateurConsole.Core
{
    public class SalesHandler
    {
        DataEncapsulator Data;
        private SalesHandler() { }
        public SalesHandler(DataEncapsulator Data)
        {
            this.Data = Data;
        }

        public IEnumerable<Sale> Sales
        {
            get
            {
                return from s in Data.DB.Sales
                       orderby s.SalesID ascending
                       select s;
            }
        }

        public void Remove(int ID)
        {
            var sale = Data.DB.Sales.First(S => S.SalesID == ID);
            Data.DB.Sales.DeleteOnSubmit(sale);
            Data.DB.SubmitChanges();
        }

        public void Edit(int ID, decimal amount, string account)
        {
            Sale sale;
            try
            {
                sale = Data.DB.Sales.First(S => S.SalesID == ID);
                sale.Amount = amount;
                sale.Account = account;
                Data.DB.SubmitChanges();
            }
            catch
            {
                sale = new Sale();
                sale.SalesID = ID;
                sale.Amount = amount;
                sale.Account = account;
                sale.Date = DateTime.Now;

                Data.DB.Sales.InsertOnSubmit(sale);
                Data.DB.SubmitChanges();
            }
            
        }

        public int Add(int SalesID, decimal amount, DateTime d, string account)
        {
            Sale newSale = new Sale();
            newSale.SalesID = SalesID;
            newSale.Date = d;
            newSale.Amount = amount;
            newSale.Account = account;

            Data.DB.Sales.InsertOnSubmit(newSale);
            Data.DB.SubmitChanges();

            return SalesID;
        }

        public IEnumerable<Sale> Filter(DateTime fromDate)
        {
            return from sale in Data.DB.Sales
                   where sale.Date.Month == fromDate.Month && sale.Date.Year == fromDate.Year
                   orderby sale.SalesID ascending
                   select sale;

        }

        public IEnumerable<Sale> FilterOneDay(DateTime d)
        {
            return from sale in Data.DB.Sales
                   where sale.Date.Year == d.Year && sale.Date.Month == d.Month && sale.Date.Day == d.Day
                   orderby sale.SalesID ascending
                   select sale;
        }

        public IEnumerable<Sale> FilterTodays()
        {
            return from sale in Data.DB.Sales
                   where sale.Date.Month == DateTime.Now.Month && sale.Date.Day == DateTime.Now.Day && sale.Date.Year == DateTime.Now.Year
                   orderby sale.SalesID ascending
                   select sale;
        }

        public IEnumerable<Sale> GetBillForSalesID(int p)
        {
            return from sale in Data.DB.Sales
                   where sale.SalesID == p
                   orderby sale.SalesID ascending
                   select sale;
        }

        public IEnumerable<Sale> SearchAccount(string SearchAccount)
        {
            return from sale in Data.DB.Sales
                   where sale.Account.ToLower().IndexOf(SearchAccount.ToLower()) != -1
                   orderby sale.SalesID ascending
                   select sale;
        }
    }
}
